#include "timer_description.h"

#include <cstdio>
#include <ctime>
#include <sys/timerfd.h>
#include <unistd.h>

TimerDescription::TimerDescription()
{
    m_timer_fd = -1;
}

TimerDescription::~TimerDescription()
{
    TimerDescription::CloseDescription();
}

int TimerDescription::DescriptionData()
{
    return m_timer_fd;
}

int TimerDescription::CreateDescription()
{
    int timer_fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK);
    int result = 0;
    if (timer_fd >= 0)
    {
        m_timer_fd = timer_fd;
        result = 1;
    }
    else
    {
        printf("create timer failed.\n");
    }
    return result;
}

void TimerDescription::CloseDescription()
{
    if (m_timer_fd >= 0)
    {
        close(m_timer_fd);
        m_timer_fd = -1;
    }
}
